﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACP.DTO;

namespace ACP.Data
{
    public class TopicsRepository: Repository<Topic>
    {
        public static void InsertTopic(Topic topic)
        {
            Repository<Topic> topicRepository = new Repository<Topic>();
            try
            {
                topicRepository.Add(topic);
                topicRepository.UnitOfWork.Commit();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                topicRepository.Dispose();
            }
        }

        public static void UpdatetTopic(Topic topicObject)
        {
            Repository<Topic> topicsRepository = new Repository<Topic>();
            try
            {
                topicsRepository.Update(topicObject);
                topicsRepository.UnitOfWork.Commit();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                topicsRepository.Dispose();
            }
        }

        public static Topic GetTopicById(int topicId)
        {
            IRepository<Topic> topicsRepository = new Repository<Topic>();
            Topic topicEntity;
            try
            {
                topicEntity = topicsRepository.First(t => t.TopicID == topicId && t.status == true);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                topicsRepository.Dispose();
            }
            return topicEntity;
        }

        public static List<Topic> GetTopicsByParentTopicId(int topicId)
        {
            Repository<Topic> topicsRepository = new Repository<Topic>();
            List<Topic> lsttopicEntity;
            try
            {
                lsttopicEntity = topicsRepository.Find(t => t.ParentTopicID == topicId && t.status == true).ToList();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                topicsRepository.Dispose();
            }
            return lsttopicEntity;
        }

        public static List<Topic> GetTopics(System.Linq.Expressions.Expression<Func<Topic, bool>> whereExpression)
        {
            Repository<Topic> topicsRepository = new Repository<Topic>();
            List<Topic> lsttopicEntity;
            try
            {
                lsttopicEntity = topicsRepository.Find(whereExpression).ToList();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                topicsRepository.Dispose();
            }
            return lsttopicEntity;
        }

        public static void InsertSubTopics(List<Topic> lsttopics)
        {
            Repository<Topic> topicRepository = new Repository<Topic>();
            try
            {
                topicRepository.AddObjects(lsttopics);
                topicRepository.UnitOfWork.Commit();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                topicRepository.Dispose();
            }
        }
    }
}
